package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TIareacontent;
import com.zmht.beike.po.TIweekinfo;
import com.zmht.specedu.vo.Teacher;
/**
 * 个别领域目标管理
 * @author fengbo
 *
 */
public class IAreaContentAction extends BaseAction {
	/**
	 * 初始化页面显示
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initAreaContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String wid = request.getParameter("wid");
		TIweekinfo info = this.getIweekinfoBiz().findById(new Integer(wid));
		
		request.getSession().setAttribute("iweekinfo", info);
		Integer aimid = info.getIaimId();
		TIaiminfo iaiminfo = this.getIaiminfoBiz().findById(aimid);
		request.getSession().setAttribute("iaiminfo", iaiminfo);
		
		
		return mapping.findForward("newGebielinyu");
	}
	/**
	 * 获得
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAreaContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String areaId = request.getParameter("areaId");
		String age = "1";
	
		TIweekinfo weekinfo =(TIweekinfo)request.getSession().getAttribute("iweekinfo"); //this.getIweekinfoBiz().findById(new Integer(wid));
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
	
		List<TIareacontent> list =this.getIareaContentBiz().getAreaContent(teacher.getTeacherId(), new Integer(areaId), (index-1)*pageSize, pageSize);
		int total = this.getIareaContentBiz().getAreaContentTotal(teacher.getTeacherId(), new Integer(areaId));

		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		if(list!=null&&list.size()>0){age = list.get(0).getAge()+"";}
		request.setAttribute("age", age);
		request.setAttribute("contents", list);
		request.setAttribute("weekinfo",weekinfo);
		request.setAttribute("areaId", areaId);
		return mapping.findForward("newGebielinyu_content");
	}
	/**
	 * 保存领域内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveAreaContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		try{
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		TIweekinfo weekinfo =(TIweekinfo)request.getSession().getAttribute("iweekinfo"); 
		TIaiminfo iaiminfo = (TIaiminfo)request.getSession().getAttribute("iaiminfo");
		TIareacontent content = new TIareacontent();
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String week = request.getParameter("week");
			String month = request.getParameter("month");
			
			content.setMonth(new Integer(month));
			content.setWeek(new Integer(week));
			
			iaiminfo = this.getIaiminfoBiz().findById(new Integer(targetId));
		
		}else{
			content.setMonth(weekinfo.getImonthinfo().getMonthType());
		}
	
		String areaId = request.getParameter("areaId");
		String age = request.getParameter("age");
		String iareaContent = request.getParameter("iareaContent");
		
		content.setAge(new Integer(age));
		content.setIareaContent(iareaContent);
		//content.setIareaId(new Integer(areaId));
		content.setIaimId(iaiminfo.getId()+"");
		if(null!=weekinfo){
			content.setWeek(weekinfo.getWeek());
		}
		content.setIareaType(areaId);
		if(null!=weekinfo){
			content.setIweekId(weekinfo.getId());
		}
		
		content.setTeacherId(teacher.getTeacherId());
		this.getIareaContentBiz().saveAreaContent(content);
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String week = request.getParameter("week");
			String month = request.getParameter("month");
			return new ActionForward("/beike/iareaContent.do?method=getAreaContentEdit&areaId="+areaId+"&targetId="+targetId+"&month="+month+"&week="+week);
		}
		
		return new ActionForward("/beike/iareaContent.do?method=getAreaContent&areaId="+areaId);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 删除内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delAreaContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String conid = request.getParameter("conid");
		String areaId = request.getParameter("areaId");
		this.getIareaContentBiz().deleteAreaContent(new Integer(conid));
		
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String week = request.getParameter("week");
			String month = request.getParameter("month");
			return new ActionForward("/beike/iareaContent.do?method=getAreaContentEdit&areaId="+areaId+"&targetId="+targetId+"&month="+month+"&week="+week);
		}

		return new ActionForward("/beike/iareaContent.do?method=getAreaContent&areaId="+areaId);
	}
	/**
	 * 更新内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateAreaContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cid = request.getParameter("cid");
		String areaId = request.getParameter("areaId");
		String iareaContent = request.getParameter("iareaContent");
		
		TIareacontent content = this.getIareaContentBiz().findById(new Integer(cid));
		content.setIareaContent(iareaContent);
		this.getIareaContentBiz().updateAreaContent(content);
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String week = request.getParameter("week");
			String month = request.getParameter("month");
			return new ActionForward("/beike/iareaContent.do?method=getAreaContentEdit&areaId="+areaId+"&targetId="+targetId+"&month="+month+"&week="+week);
		}
		return new ActionForward("/beike/iareaContent.do?method=getAreaContent&areaId="+areaId);
	}
	/**
	 * 编辑目标的时候 获得领域内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAreaContentEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String areaId = request.getParameter("areaId");
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		String week = request.getParameter("week");
		
		String age = "1";
	
		TIweekinfo weekinfo = this.getIweekinfoBiz().findByTarget(targetId, month, week,teacher.getTeacherId());//(TIweekinfo)request.getSession().getAttribute("iweekinfo"); //this.getIweekinfoBiz().findById(new Integer(wid));
		//this.getIweekinfoBiz().findByTarget(targetId, month, week);
		
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
	
		//List<TIareacontent> list = this.getIareaContentBiz().getAreaContent(teacher.getTeacherId(), new Integer(areaId), (index-1)*pageSize, pageSize);
		//int total = this.getIareaContentBiz().getAreaContentTotal(teacher.getTeacherId(), new Integer(areaId));

		List<TIareacontent> list = this.getIareaContentBiz().getAreaContentEdit(new Integer(targetId),teacher.getTeacherId(), new Integer(month), new Integer(week), new Integer(areaId), (index-1)*pageSize, pageSize);
		int total = this.getIareaContentBiz().getAreaContentEditTotal(new Integer(targetId),teacher.getTeacherId(), new Integer(month), new Integer(week), new Integer(areaId));
		
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		if(list!=null&&list.size()>0){age = list.get(0).getAge()+"";}
		request.setAttribute("age", age);
		request.setAttribute("contents", list);
		request.setAttribute("iweekinfo",weekinfo);
		
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("month", month);
		request.setAttribute("week", week);
		request.setAttribute("edit", "1");
		request.setAttribute("areaId", areaId);
		
		return mapping.findForward("newGebielinyu_content");
	}
}
